/*
 * PACKAGE NAME: com.jodinfo.outmirror.api.officialusr.service
 * FILE    NAME: OfficialUsrDaoImpl.java
 * COPYRIGHT: Copyright(c) © 2016 heroj Company Ltd. All Rights Reserved
 */ 
package com.jodinfo.outmirror.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jodinfo.outmirror.entity.OfficialUsrEntity;
import com.jodinfo.outmirror.service.OfficialUsrBizService;
import com.jodinfo.outmirror.service.OfficialUsrDmlService;
import com.jodinfo.outmirror.service.OfficialUsrQueryService;
import com.jodinfo.outmirror.util.DateUtils;
import com.jodinfo.outmirror.util.ResponseEntity;
import com.jodinfo.outmirror.util.StringUtils;
import com.jodinfo.outmirror.util.SysConstant;

@Service(value="officialUsrBizService")
public class OfficialUsrBizServiceImpl implements OfficialUsrBizService{
    private static final Logger logger = LoggerFactory.getLogger(OfficialUsrBizServiceImpl.class);
 	
 	@Resource(name="officialUsrDmlService")
	private OfficialUsrDmlService officialUsrDmlService;
	@Resource(name="officialUsrQueryService")
	private OfficialUsrQueryService officialUsrQueryService;
	
	/** 
	 * 
	 * @author zhuxiaoliu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.officialusr.OfficialUsrBizService#addOfficialUsr(OfficialUsrEntity officialUsrEntity)
	 * @param OfficialUsrEntity officialUsrEntity
	 * @return int
	 */
	public ResponseEntity addOfficialUsr(OfficialUsrEntity officialUsrEntity) {
			ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
			  	 int i =  officialUsrDmlService.addOfficialUsr(officialUsrEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(Exception e){
				String errorMessage = e.getMessage();
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author zhuxiaoliu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.officialusr.OfficialUsrBizService#updateOfficialUsr(OfficialUsrEntity officialUsrEntity)
	 * @param OfficialUsrEntity officialUsrEntity
	 * @return ResponseEntity
	 */
	public ResponseEntity updateOfficialUsr(OfficialUsrEntity officialUsrEntity) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = officialUsrDmlService.updateOfficialUsr(officialUsrEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(Exception e){
				String errorMessage = e.getMessage();
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author zhuxiaoliu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.officialusr.OfficialUsrBizService#queryAllOfficialUsr(OfficialUsrEntity officialUsrEntity)
	 * @param OfficialUsrEntity officialUsrEntity
	 * @return String
	 */
	public String queryAllOfficialUsr(OfficialUsrEntity officialUsrEntity) {
		return JSON.toJSONString(officialUsrQueryService.queryAllOfficialUsr(officialUsrEntity));
	} 
	/** 
	 * 
	 * @author zhuxiaoliu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.officialusr.OfficialUsrBizService#queryAllOfficialUsr(OfficialUsrEntity officialUsrEntity)
	 * @param OfficialUsrEntity officialUsrEntity
	 * @return PageResult
	 */
	public String queryListForPageOfficialUsr(OfficialUsrEntity officialUsrEntity) {
		//return new PageResult( officialUsrQueryService.queryListForPageOfficialUsr(officialUsrEntity), officialUsrQueryService.queryCountOfficialUsr(officialUsrEntity)).toString();
		return "";
	} 
	/** 
	 * 
	 * @author zhuxiaoliu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.officialusr.OfficialUsrQueryService#queryById(OfficialUsrEntity officialUsrEntity)
	 * @param BigInteger id
	 * @return
	 */
	public OfficialUsrEntity queryById(BigInteger id) {
		return officialUsrQueryService.queryById(id);
	}
	
	public void exportOfficialUsr(OfficialUsrEntity officialUsrEntity, HttpServletResponse response) {
		List<OfficialUsrEntity> exportList = null;
		String ids = officialUsrEntity.getIds();
		if (StringUtils.isEmpty(ids)) {
			exportList =officialUsrQueryService.queryAllOfficialUsr(officialUsrEntity);
		} else {
			String[] idArray = ids.split(",");
			List<BigInteger> idList = new ArrayList<BigInteger>();
			for(String id : idArray) {
				idList.add(new BigInteger(id));
			}
			exportList = officialUsrQueryService.queryByIds(idList);
			// 将字典中的code转换为name
		   //convertCodeToName(exportList);
		}
		//ExcelExport.exportExcel(exportList, "OfficialUsrEntityExcel", "OfficialUsrEntity信息", response);
	}
	
	/** 
	 * 批量删除 数据处理
	 * @author zhuxiaoliu
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.officialusr.OfficialUsrQueryService#queryById(OfficialUsrEntity officialUsrEntity)
	 * @param BigInteger id
	 * @return
	 */
	public ResponseEntity batchDelOfficialUsr(OfficialUsrEntity officialUsrEntity) {
		String ids = officialUsrEntity.getIds();
		String[] idArray = ids.split(",");
		List<OfficialUsrEntity> officialUsrEntityList = new ArrayList<OfficialUsrEntity>();
		for(String id : idArray) {
			OfficialUsrEntity officialUsrModel = new OfficialUsrEntity();
			officialUsrModel.setId(new BigInteger(id));
		//	officialUsrModel.setLastUpdateUsrName(UserContext.getUser().getName());
			officialUsrModel.setLastUpdateTime(DateUtils.getCurrentTime());
			officialUsrModel.setIsDel(SysConstant.IS_DEL_Y);
			officialUsrEntityList.add(officialUsrModel);
		}
		//return batchUpdateOfficialUsr(officialUsrEntityList);
		return null;
	}
	
	/**
	 * 执行批量删除操作
	 * @author zhuxiaoliu
	 * @date 2017年3月13日 上午11:40:31
	 * @param officialUsrList
	 * @return
	 */
//	public ResponseEntity batchUpdateOfficialUsr(List<OfficialUsrEntity> officialUsrList) {
//		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
//			try{
//				int i = officialUsrDmlService.batchUpdateOfficialUsr(officialUsrList);
//				 rs.setMessage(i+"条记录操作成功");
//			 }catch(AbstractGenericException e){
//				logger.error(e.getFullMessage());
//				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
//			}catch(Exception e){
//				String errorMessage = StringUtils.toString(e);
//				logger.error(errorMessage);
//				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
//			}
//		return rs;
//	}
	
	
	/**
	 * 
	 * 执行批插入操作
	 * @author zhuxiaoliu
	 * @date 2017年3月13日 上午11:37:14
	 * @param {moduleName}List
	 * @return
	 */
//	public ResponseEntity batchAddOfficialUsr(List<OfficialUsrEntity> officialUsrList) {
//		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
//		try{
//			int i = officialUsrDmlService.batchAddOfficialUsr(officialUsrList);
//			 rs.setMessage(i+"条记录插入操作成功");
//		 }catch(AbstractGenericException e){
//			logger.error(e.getFullMessage());
//			rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
//		}catch(Exception e){
//			String errorMessage = StringUtils.toString(e);
//			logger.error(errorMessage);
//			rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
//		}
//		return rs;
//	}
	
	public ResponseEntity queryByEntity(OfficialUsrEntity officialUsrEntity){
		ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS,"登录成功");
		OfficialUsrEntity queryEntity = new OfficialUsrEntity();
		queryEntity.setLoginName(officialUsrEntity.getLoginName());
		OfficialUsrEntity resOfficialUsrEntity = officialUsrQueryService.queryByEntity(queryEntity);
		if (resOfficialUsrEntity == null) {
			rs.setStatus(SysConstant.FAIL).setMessage("用户不存在");
		}else{
			String md5Psd = StringUtils.encoderByMd5(officialUsrEntity.getPassword().trim());
			if (!md5Psd.equals(resOfficialUsrEntity.getPassword())) {
				rs.setStatus(SysConstant.FAIL).setMessage("密码不正确");
			}
		}
		
		return rs;
	}
}

